/**
 * 按钮级权限控制指令
 * 
 * @desc 使用 v-permission="'system:user:add'"
 * @desc 使用 v-permission="['system:user:add', 'system:user:edit']"
 * @desc 使用 v-any-permission="['system:user:add', 'system:user:edit']"
 */
import type { App } from 'vue'
import { useUserStore } from '@/stores'

export default {
  install(app: App) {
    app.directive('permission', {
      mounted: (el, binding) => {
        const { hasPermission } = useUserStore()
        if (!hasPermission(binding.value)) {
          el.parentNode?.removeChild?.(el)
        }
      },
    })
    app.directive('any-permission', {
      mounted: (el, binding) => {
        const { hasAnyPermission } = useUserStore()
        if (!hasAnyPermission(binding.value)) {
          el.parentNode?.removeChild?.(el)
        }
      },
    })
  },
}
